import { sys } from 'cc';

/**
 * Android平台桥接工具类
 */
export class AndroidBridge {
    
    /**
     * 从Android SharedPreferences读取字符串值
     */
    static getStringFromSharedPreferences(key: string, defaultValue: string = ""): string {
        if (sys.platform !== sys.Platform.ANDROID) {
            console.warn('AndroidBridge: 当前平台不是Android，返回默认值');
            return defaultValue;
        }
        
        try {
            // 调用Java方法读取SharedPreferences
            const result = (jsb as any).reflection.callStaticMethod(
                "com/cocos/game/AndroidBridge",
                "getStringFromSharedPreferences",
                "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
                key, defaultValue
            );
            
            console.log(`AndroidBridge: 读取 ${key} = ${result}`);
            return result || defaultValue;
            
        } catch (error) {
            console.error('AndroidBridge: 读取SharedPreferences失败', error);
            return defaultValue;
        }
    }
    
    /**
     * 从Android SharedPreferences读取布尔值
     */
    static getBooleanFromSharedPreferences(key: string, defaultValue: boolean = false): boolean {
        if (sys.platform !== sys.Platform.ANDROID) {
            return defaultValue;
        }
        
        try {
            const result = (jsb as any).reflection.callStaticMethod(
                "com/cocos/game/AndroidBridge",
                "getBooleanFromSharedPreferences",
                "(Ljava/lang/String;Z)Z",
                key, defaultValue
            );
            
            console.log(`AndroidBridge: 读取 ${key} = ${result}`);
            return result;
            
        } catch (error) {
            console.error('AndroidBridge: 读取Boolean失败', error);
            return defaultValue;
        }
    }
    
    /**
     * 获取Token
     */
    static getToken(): string {
        return this.getStringFromSharedPreferences("token", "");
    }
    
    /**
     * 获取服务器地址
     */
    static getServerUrl(): string {
        return this.getStringFromSharedPreferences("serverUrl", "");
    }
    
    /**
     * 获取调试模式
     */
    static getDebugMode(): boolean {
        return this.getBooleanFromSharedPreferences("debug", false);
    }
    
    /**
     * 通知Android游戏事件
     */
    static notifyGameEvent(event: string, data?: any): void {
        if (sys.platform !== sys.Platform.ANDROID) {
            return;
        }
        
        try {
            const dataStr = data ? JSON.stringify(data) : "";
            (jsb as any).reflection.callStaticMethod(
                "com/cocos/game/AndroidBridge",
                "notifyGameEvent",
                "(Ljava/lang/String;Ljava/lang/String;)V",
                event, dataStr
            );
            
            console.log(`AndroidBridge: 发送事件 ${event}, 数据: ${dataStr}`);
            
        } catch (error) {
            console.error('AndroidBridge: 发送事件失败', error);
        }
    }
    
    /**
     * 发送按钮点击事件
     */
    static notifyButtonClick(buttonId: string, buttonText?: string, extraData?: any): void {
        const eventData = {
            buttonId: buttonId,
            buttonText: buttonText || "",
            timestamp: Date.now(),
            extraData: extraData || {}
        };
        
        this.notifyGameEvent("buttonClick", eventData);
    }
    
    /**
     * 发送UI事件
     */
    static notifyUIEvent(eventType: string, elementId: string, eventData?: any): void {
        const data = {
            eventType: eventType,
            elementId: elementId,
            timestamp: Date.now(),
            data: eventData || {}
        };
        
        this.notifyGameEvent("uiEvent", data);
    }
    
    /**
     * 发送游戏状态变化事件
     */
    static notifyGameStateChange(newState: string, oldState?: string, stateData?: any): void {
        const data = {
            newState: newState,
            oldState: oldState || "",
            timestamp: Date.now(),
            stateData: stateData || {}
        };
        
        this.notifyGameEvent("gameStateChange", data);
    }
    
    /**
     * 发送用户操作事件
     */
    static notifyUserAction(action: string, target?: string, actionData?: any): void {
        const data = {
            action: action,
            target: target || "",
            timestamp: Date.now(),
            actionData: actionData || {}
        };
        
        this.notifyGameEvent("userAction", data);
    }
} 